/*
 * BanBu_DialogueCell
 * @description 本文件提供对话列表的对话定制cell
 * @package
 * @author 		***
 * @copyright
 * @version 		1.0
 * @description 本文件提供对话列表的对话定制cell
 */


#import "BanBu_DialogueCell.h"
#import <QuartzCore/QuartzCore.h>
#import "AppDataManager.h"
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"
#import "BanBu_ImageView.h"
#import "BanBu_ThemeManager.h"

#define x 90
#define boyColor [UIColor colorWithRed:48.0/255 green:169.0/255 blue:217.0/255 alpha:1.0]
#define girlColor [UIColor colorWithRed:252.0/255 green:192.0/255 blue:213.0/255 alpha:1.0]

@implementation BanBu_DialogueCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        // cell背景
        BanBu_ImageView *backImage = [[BanBu_ImageView alloc] initWithBackGroundImageType:UIImageViewCreatTypeCell];
        backImage.frame = CGRectMake(0, 0, 320, 88);
        self.backgroundView = backImage;
        [backImage release];

        //cell分割线
        lineView = [[UIImageView alloc] initWithFrame:CGRectMake(-40, 83, 360, 1.0)];
        lineView.image = [ThemeManager getThemeImage:@"lineColor.png"];
        [self.contentView addSubview:lineView];
        [lineView release];
        
        //头像
        _iconView = [[UIImageView alloc] initWithFrame:CGRectMake(6,7, 69, 69)];
        _iconView.layer.cornerRadius = 34.5f;
        _iconView.layer.masksToBounds = YES;
        _iconView.contentMode = UIViewContentModeScaleAspectFit;
		[self.contentView addSubview:_iconView];
        [_iconView release];

        
        //vip标志
        _vipImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 6, 71, 73)];
        
        [self.contentView addSubview:_vipImageView];
        [_vipImageView release];
        
		/*这是未读消息的badagevalue*/
 
        //用户姓名
		UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x,7, 140, 20)];
        _nameLabel = label;
		_nameLabel.textAlignment = kTextAlignmentLeft;
		_nameLabel.backgroundColor = [UIColor clearColor];
		_nameLabel.font = [UIFont boldSystemFontOfSize:16];
		[self.contentView addSubview:_nameLabel];
        [label release];
        
        //最后发送时间
        _lastTimeLabel = [[UILabel alloc] initWithFrame:CGRectMake(190, 8, 120, 15)];
        _lastTimeLabel.backgroundColor = [UIColor clearColor];
		_lastTimeLabel.font = [UIFont systemFontOfSize:13];
		_lastTimeLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _lastTimeLabel.textAlignment=kTextAlignmentRight;
		[self.contentView addSubview:_lastTimeLabel];
        [_lastTimeLabel release];

        
        _distanceBack = [[UIImageView alloc]initWithFrame:CGRectMake(87, 36, 10, 14)];
        NSString *signatureBackpath = [[NSBundle mainBundle] pathForResource:@"distance_mark" ofType:@"png"];
        _distanceBack.image =  [UIImage imageWithContentsOfFile:signatureBackpath];
        [self.contentView addSubview:_distanceBack];
        [_distanceBack release];
        _distanceLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 34, 47, 16)];
        _distanceLabel.textAlignment = kTextAlignmentLeft;
		_distanceLabel.backgroundColor = [UIColor clearColor];
		_distanceLabel.font = [UIFont systemFontOfSize:13];
		_distanceLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
		[self.contentView addSubview:_distanceLabel];
        [_distanceLabel release];
        
        //年龄和性别
        _sexImgV = [[UIImageView alloc]initWithFrame:CGRectMake(145, 36, 13, 13)];
        [self.contentView addSubview:_sexImgV];
        [_sexImgV release];
        
        _ageLabel = [[UILabel alloc]initWithFrame:CGRectMake(161, 34, 50, 16)];
        _ageLabel.backgroundColor = [UIColor clearColor];
		_ageLabel.font = [UIFont systemFontOfSize:13];
		_ageLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
		[self.contentView addSubview:_ageLabel];
        [_ageLabel release];

        NSString *verifypath = [[NSBundle mainBundle] pathForResource:@"verify_card" ofType:@"png"];
        _verify = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile:verifypath]];
        _verify.frame = CGRectMake(188, 37.5, 15, 10);
        _verify.hidden = YES;
        [self.contentView addSubview:_verify];
        [_verify release];
        
        /*这是状态的按钮的*/
        
        //送达、已读、失败等状态的背景
        _readAndsend=[[UIImageView alloc]initWithFrame:CGRectMake(x+20+33, 60, 25, 14)];
        [self.contentView addSubview:_readAndsend];
        [_readAndsend release];
        
        //送达、已读、失败等状态
        _readAndSendLabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 25, 14)];
        _readAndSendLabel.backgroundColor=[UIColor clearColor];
        _readAndSendLabel.textColor=[UIColor whiteColor];
        _readAndSendLabel.font=[UIFont systemFontOfSize:11];
        _readAndSendLabel.textAlignment=kTextAlignmentCenter;
        [_readAndsend addSubview:_readAndSendLabel];
        [_readAndSendLabel release];
        
        //最后一条打招呼对话的人的名字
        UILabel *alabel = [[UILabel alloc] initWithFrame:CGRectMake(x-3, 63, 50, 14)];
        _nameLabelSayHi = alabel;
		_nameLabelSayHi.textAlignment = kTextAlignmentLeft;
		_nameLabelSayHi.backgroundColor = [UIColor clearColor];
		_nameLabelSayHi.font = [UIFont systemFontOfSize:13];
		[self.contentView addSubview:_nameLabelSayHi];
        [alabel release];
        
        //对话内容
		_lastDialogueLabel = [[UILabel alloc] initWithFrame:CGRectMake(x+18,60, 202, 20)];
		_lastDialogueLabel.backgroundColor = [UIColor clearColor];
        _lastDialogueLabel.numberOfLines = 0;
		_lastDialogueLabel.font = [UIFont systemFontOfSize:13];
		_lastDialogueLabel.textColor = [UIColor darkGrayColor];
		[self.contentView addSubview:_lastDialogueLabel];
        [_lastDialogueLabel release];
        
//        //接收、发送标识
//        _receiveAndsend=[UIButton buttonWithType:UIButtonTypeCustom];
//        _receiveAndsend.frame=CGRectMake(x-3, 57, 16, 16);
//        [self.contentView addSubview:_receiveAndsend];

    }
    return self;
}

-(void)setVIPFlag:(BOOL)_flag
{
    if (_flag)
    {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"verify_vip" ofType:@"png"];
        _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
    }

}

//设置加v
- (void)setVerify:(BOOL)_flag
{
    _verify.hidden = _flag;
}

//设置是接收状态还是发送状态
-(void)setReceiveAndsend11:(BOOL)isMe
{
//    if(isMe)
//    {
//        [_receiveAndsend setBackgroundImage:[UIImage imageNamed:@"send.png"] forState:UIControlStateNormal];
//        [_receiveAndsend setBackgroundImage:[UIImage imageNamed:@"send.png"] forState:UIControlStateHighlighted];
//    }
//    else
//    {
//        [_receiveAndsend setBackgroundImage:[UIImage imageNamed:@"receive.png"] forState:UIControlStateNormal];
//        [_receiveAndsend setBackgroundImage:[UIImage imageNamed:@"receive.png"] forState:UIControlStateHighlighted];
//    }
}

//设置接收、发送按钮隐藏
-(void)setReceiveAndSendButtonHidden:(BOOL)isHidden
{
//    _receiveAndsend.hidden = isHidden;
}

//设置发送状态（发送中、送达、已读、失败、无）
-(void)setReadAndSend:(DialogueMessageStautusType)type
{
    switch (type)
    {
        case DialogueMessageStausFailType:
        {
            _readAndsend.hidden=NO;
            _readAndSendLabel.hidden=NO;
            _readAndsend.image=[UIImage imageNamed:@"icon_msgfail1.png"];
            
            _readAndSendLabel.text= NSLocalizedString(@"newsState4", nil);
            
            CGFloat size=[NSLocalizedString(@"newsState4", nil) sizeWithFont:[UIFont systemFontOfSize:11]].width;
            _readAndsend.frame =  CGRectMake(x-3, 62, size, 15);
//            _readAndsend.frame=CGRectMake(x+20+35, 35, size, 15);
            
            //   修改对话的位置2014/07/14,隐藏了_readAndsend
            [_lastDialogueLabel setFrame:CGRectMake(x-3,60, 220, 20)];
            _readAndSendLabel.frame=CGRectMake(0, 0, size, 15);
        }
            break;
        //送达
        case DialogueMessageStautusSentType:
        {
            _readAndsend.hidden=NO;
            _readAndSendLabel.hidden=NO;
            _readAndsend.image=[UIImage imageNamed:@"icon_msgsent1.png"];
                        
            _readAndSendLabel.text=NSLocalizedString(@"newsState", nil);
            
            CGFloat size=[NSLocalizedString(@"newsState", nil) sizeWithFont:[UIFont systemFontOfSize:11]].width;
            
            _readAndsend.frame =  CGRectMake(x-3, 62, size, 15);
            //            _readAndsend.frame=CGRectMake(x+20+35, 35, size, 15);
            //   修改对话的位置2014/07/14
            [_lastDialogueLabel setFrame:CGRectMake(x + size + 2,60, 220 - size, 20)];
            _readAndSendLabel.frame=CGRectMake(0, 0, size, 15);
        }
            break;
        //未读
        case DialogueMessageStautusSendingType:
        {
            _readAndsend.hidden=NO;
            _readAndSendLabel.hidden=NO;
            _readAndsend.image=[UIImage imageNamed:@"icon_msgpend1.png"];
            
            _readAndSendLabel.text= NSLocalizedString(@"newsState3", nil);
            
            CGFloat size=[NSLocalizedString(@"newsState3", nil) sizeWithFont:[UIFont systemFontOfSize:11]].width;
            
            _readAndsend.frame =  CGRectMake(x-3, 62, size, 15);
            //            _readAndsend.frame=CGRectMake(x+20+35, 35, size, 15);
            //   修改对话的位置2014/07/14
            [_lastDialogueLabel setFrame:CGRectMake(x + size + 2,60, 220 - size, 20)];
            _readAndSendLabel.frame=CGRectMake(0, 0, size, 15); 
        }
            break;
        //已读
        case DialogueMessageStautusReadType:
        {
            _readAndsend.hidden=NO;
            _readAndSendLabel.hidden=NO;

            _readAndsend.image=[UIImage imageNamed:@"icon_msgread1.png"];
            
            _readAndSendLabel.text=NSLocalizedString(@"newsState1", nil);
            
            CGFloat size=[NSLocalizedString(@"newsState1", nil) sizeWithFont:[UIFont systemFontOfSize:11]].width;
            
            _readAndsend.frame =  CGRectMake(x-3, 62, size, 15);
            //            _readAndsend.frame=CGRectMake(x+20+35, 35, size, 15);
            //   修改对话的位置2014/07/14
            [_lastDialogueLabel setFrame:CGRectMake(x + size + 2,60, 220 - size, 20)];
            _readAndSendLabel.frame=CGRectMake(0, 0, size, 15);                        
        }
            break;
        //不显示
        case DialogueMessageStautusNoneType:
        {
            _readAndsend.hidden=YES;
            _readAndSendLabel.hidden=YES;
            [_lastDialogueLabel setFrame:CGRectMake(x ,60, 220, 20)];
        }
            break;
        default:
            break;
    }
}
  
//设置头像
- (void)setAvatar:(NSString *)avatarUrlStr
{
    [_iconView setImageWithURL:[NSURL URLWithString:avatarUrlStr] placeholderImage:[UIImage imageNamed:@"default.png"] andType:1];
}

//设置昵称
- (void)setName:(NSString *)name
{
    _nameLabel.text = name;
}

//设置距离
-(void)setDistance:(NSString *)distance
{
    if (distance && distance.length > 0)
    {
        _distanceLabel.text=distance;
        [_distanceBack setHidden:NO];
    }
    else
    {
        _distanceLabel.text =@"";
        [_distanceBack setHidden:YES];
    }
    [_distanceLabel sizeToFit];
    [_verify setFrame:CGRectMake(150 + _distanceLabel.frame.size.width, 37.5, 15, 10)];
}

- (void)setAge:(NSString *)age sex:(BOOL)sex verify:(BOOL)verify
{
    [_verify setHidden:!verify];
    _ageLabel.text = age;
    [_sexImgV setFrame:CGRectMake(100 + _distanceLabel.frame.size.width + 10, 36, 13, 13)];
    [_ageLabel setFrame:CGRectMake(116 + _distanceLabel.frame.size.width + 10, 34, 50, 16)];
    switch (sex)
    {
        case 1:
        {
            NSString *sexpath = [[NSBundle mainBundle] pathForResource:@"m" ofType:@"png"];
            _sexImgV.image =  [UIImage imageWithContentsOfFile:sexpath];
            if (!verify)
            {
                NSString *path = [[NSBundle mainBundle] pathForResource:@"verify_normal" ofType:@"png"];
                _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
                
            }
            else
            {
                
                NSString * verifySource = @"verify_m";
                NSString *path = [[NSBundle mainBundle] pathForResource:verifySource ofType:@"png"];
                _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
            }

        }
            break;
        case 0:
        {
            NSString *sexpath = [[NSBundle mainBundle] pathForResource:@"f" ofType:@"png"];
            _sexImgV.image =  [UIImage imageWithContentsOfFile:sexpath];
            if (!verify)
            {
                NSString *path = [[NSBundle mainBundle] pathForResource:@"verify_normal" ofType:@"png"];
                _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
                
            }
            else
            {
                
                NSString * verifySource = @"verify_f";
                NSString *path = [[NSBundle mainBundle] pathForResource:verifySource ofType:@"png"];
                _vipImageView.image =  [UIImage imageWithContentsOfFile:path];
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)setLastInfo:(NSString *)infoStr
{
    _lastTimeLabel.text = [infoStr substringWithRange:NSMakeRange(5, infoStr.length-5-3)]; 
}

- (void)setlastDialogue:(NSString *)content andType:(NSInteger)type;
{
    _lastDialogueLabel.text = NSLocalizedString(content, nil);
//    if(type == ChatCellTypeEmi)
//    {
//        int t=[MyAppDataManager.emiNameArr indexOfObject:content];
//        if(t<38)
//        {
//            _lastDialogueLabel.text = [MyAppDataManager.emiLanguageArr objectAtIndex:t];
//        }
//    }
//    else
//    {
//        _lastDialogueLabel.text = NSLocalizedString(content, nil);
//    }
}

// 出现badageView
-(void)setBadageValue:(NSString *)badageValue
{
    self.badgeView = (UIBadgeView *)[self viewWithTag:1000];
    if(!badageValue || ![badageValue intValue])
    {
        if(self.badgeView != nil)
        {
            self.badgeView.hidden = YES;
        }
    }
    else
    {
        float width = [badageValue sizeWithFont:[UIFont boldSystemFontOfSize:14]].width+12;
        if(!self.badgeView)
        {
            self.badgeView = [[[UIBadgeView alloc] initWithFrame:CGRectMake(58, 3, width, 20)] autorelease];
            self.badgeView.hidden = NO;
            self.badgeView.tag = 1000;
            self.badgeView.backgroundColor = [UIColor clearColor];
            self.badgeView.badgeColor = [UIColor redColor];
            [self.contentView addSubview:self.badgeView];
        }
        self.badgeView.hidden = NO;
        self.badgeView.badgeString = badageValue;
        self.badgeView.frame = CGRectMake(80 - width, 3, width, 20);
    }
}

//多选删除部分
- (void) setCheckImageViewCenter:(CGPoint)pt alpha:(CGFloat)alpha animated:(BOOL)animated
{
	if (animated)
	{
		[UIView beginAnimations:nil context:nil];
		[UIView setAnimationBeginsFromCurrentState:YES];
		[UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
		[UIView setAnimationDuration:0.3];
		
		m_checkImageView.center = pt;
		m_checkImageView.alpha = alpha;
		
		[UIView commitAnimations];
	}
	else
	{
		m_checkImageView.center = pt;
		m_checkImageView.alpha = alpha;
	}
}

//编辑状态
- (void) setEditing:(BOOL)editting animated:(BOOL)animated
{
	if (self.editing == editting)
	{
		return;
	}
    
    //当滑动删除cell时，不显示选中、为选中图标，用系统删除方式显示
//    if (_sliding)
//    {
//        return;
//    }
	[super setEditing:editting animated:animated];
	
	if (editting)
	{
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		self.textLabel.backgroundColor = [UIColor clearColor];
		self.detailTextLabel.backgroundColor = [UIColor clearColor];
        
        //2014年07月28日14:16:22 by lly 编辑时时间 距离前移，避免遮挡
		[_lastTimeLabel setFrame:CGRectMake(152, 8, 120, 15)];
       
        if (m_checkImageView == nil)
		{
			m_checkImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"CellNotSelected.png"]];
			[self addSubview:m_checkImageView];
		}
		
		[self setChecked:m_checked];
 		m_checkImageView.center = CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
											  CGRectGetHeight(self.bounds) * 0.5);
		m_checkImageView.alpha = 0.0;
		[self setCheckImageViewCenter:CGPointMake(20.5, CGRectGetHeight(self.bounds) * 0.5)
								alpha:1.0 animated:animated];
	}
	else
	{
        //2014年07月28日14:16:22 by lly 编辑完成时，时间距离放回原位置
        [_lastTimeLabel setFrame:CGRectMake(190, 8, 120, 15)];
       
		m_checked = NO;
		self.selectionStyle = UITableViewCellSelectionStyleBlue;
		
		if (m_checkImageView)
		{
			[self setCheckImageViewCenter:CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
													  CGRectGetHeight(self.bounds) * 0.5)
									alpha:0.0
								 animated:animated];
		}
	}
}

- (void)setChecked:(BOOL)checked
{
	if (checked)
	{
		m_checkImageView.image = [UIImage imageNamed:@"CellSelected.png"];
 	}
	else
	{
		m_checkImageView.image = [UIImage imageNamed:@"CellNotSelected.png"];
 	}
	m_checked = checked;
}

- (void)layoutSubviews
{
    lineView.image = [ThemeManager getThemeImage:@"lineColor.png"];
    [super layoutSubviews];
    if(self.highlighted)
    {
        _nameLabel.textColor = [UIColor whiteColor];
        _lastTimeLabel.textColor = [UIColor whiteColor];
        _lastDialogueLabel.textColor = [UIColor whiteColor];
        _nameLabelSayHi.textColor = [UIColor whiteColor];
        _distanceLabel.textColor = [UIColor whiteColor];
        _ageLabel.textColor = [UIColor whiteColor];
    }
    else
    {
        _distanceLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _ageLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _nameLabel.textColor = [UIColor blackColor];
        
        _lastTimeLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _lastDialogueLabel.textColor = UIColorWithRGB(150, 150, 150, 1.0);
        _nameLabelSayHi.textColor = UIColorWithRGB(150, 150, 150, 1.0);
    }
    if (!self.editing)
    {
        //by yinlinlin 2014年08月12日16:23:51,选中删除的对话时，移动table，点击完成，有两个cell位置不正确，修改
        [_lastTimeLabel setFrame:CGRectMake(190, 8, 120, 15)];
        
		m_checked = NO;
		self.selectionStyle = UITableViewCellSelectionStyleBlue;
		
		if (m_checkImageView)
		{
			[self setCheckImageViewCenter:CGPointMake(-CGRectGetWidth(m_checkImageView.frame) * 0.5,
													  CGRectGetHeight(self.bounds) * 0.5)
									alpha:0.0
								 animated:NO];
		}
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];
    
}

-(void)cancelImageLoad{
    
    [_iconView cancelCurrentImageLoad];
    _iconView.image = nil;
}

- (void)willMoveToSuperview:(UIView *)newSuperview {
	[super willMoveToSuperview:newSuperview];
	if(!newSuperview) {
		[self cancelImageLoad];
	}
}
#pragma mark - 最后一条打招呼的消息
-(void)setNameLastSayHi:(NSString *)pname
{
    if (pname)
    {
//        _receiveAndsend.hidden = YES;
        _nameLabelSayHi.hidden = NO;
        _nameLabelSayHi.text = [NSString stringWithFormat:@"%@:",pname];
        
        CGFloat btnLen = [_nameLabelSayHi.text sizeWithFont:[UIFont systemFontOfSize:13] constrainedToSize:CGSizeMake(150, 30)].width;

        _nameLabelSayHi.frame = CGRectMake(x-3, 62, btnLen, 14);
        _lastDialogueLabel.frame = CGRectMake(x+btnLen, 60, 225-btnLen, 20);
    }
    else
    {
//        _receiveAndsend.hidden = NO;
        _nameLabelSayHi.hidden = YES;
        _nameLabelSayHi.text = nil;
        _nameLabelSayHi.frame = CGRectMake(x-3, 62, 50, 14);
        
        //显示发送按钮_lastDialogueLabel.frame
        if ([_readAndsend isHidden])
        {
            _lastDialogueLabel.frame = CGRectMake(x,60, 202, 20);
        }
        //不显示发送按钮_lastDialogueLabel.frame
        else
        {
            _lastDialogueLabel.frame = CGRectMake(x+ 3 + _readAndsend.frame.size.width,60, 202, 20);
        }
    }
}

-(void)dealloc
{
    [_badgeView release],_badgeView = nil;
    [super dealloc];
}

@end
